package cn.xi.controller;


import cn.xi.checkItem.CheckGroupService;
import cn.xi.constant.MessageConstant;
import cn.xi.entity.PageResult;
import cn.xi.entity.QueryPageBean;
import cn.xi.entity.Result;
import cn.xi.pojo.CheckGroup;
import cn.xi.pojo.CheckItem;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/fiandpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findpage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findcheckitem")
    public List<CheckItem> findcheckitem() {
        return checkGroupService.findcheckitem();
    }

    @RequestMapping("/save")
    public Result save(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.save(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.update(checkGroup, checkitemIds);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    @RequestMapping("/findci")
    public HashMap findci(Integer id) {
        return checkGroupService.findci(id);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            checkGroupService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
         return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

}
